//
//  LCZColumnCell.m
//  PersonalNews
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZColumnCell.h"
#import "LCZLeftColumnCell.h"
#import "LCZrightColumnCell.h"
#import "LCZColumnModel.h"


#define WIDETH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
// 栏目网址
#define COLUMN @"http://c.3g.163.com/nc/topicset/ios/v4/subscribe/read/all.html"
#define TITLE

@implementation LCZColumnCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self create];
    }
    return self;
}

- (void)create
{
    self.leftTitleArr = [NSMutableArray array];
    self.mainArr = [NSMutableArray array];
    self.columnModel = [[LCZColumnModel alloc]init];
    self.bigArr = [NSMutableArray array];

    
    self.leftTableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(0, 0, 70, HEIGHT) style:UITableViewStylePlain];
    [self.contentView addSubview:self.leftTableView];
    self.leftTableView.rowHeight = 60;
    // 让tableView的线隐藏
    self.leftTableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    
    self.leftTableView.showsVerticalScrollIndicator = NO;
    
    self.leftTableView.delegate =self;
    self.leftTableView.dataSource = self;
    
    self.rightTableView = [[LCZBaseTableView alloc]initWithFrame:CGRectMake(70, 0, WIDETH - 70, HEIGHT) style:UITableViewStylePlain];
    [self.contentView addSubview:self.rightTableView];
    self.rightTableView.rowHeight = 80;
    
    self.rightTableView.delegate = self;
    self.rightTableView.dataSource = self;
    [self.rightTableView registerClass:[LCZrightColumnCell class] forCellReuseIdentifier:@"rightReuse"];
    [self createData];
    
    // 加上等待小动画
    [HUDManager showStatus];
    
}
// 请求数据
- (void)createData
{
    self.strURL = COLUMN;
    [LCZNetWorkTools getdataWithString:self.strURL block:^(id block) {
        self.bigArr = block;
        
        self.mainArr = self.bigArr[1][@"tList"];
        
        
        [self.leftTableView reloadData];
        [self.rightTableView reloadData];
        
        [HUDManager dismissHUD];
        
    }];
    
}

#pragma mark tableView的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftTableView) {
        static NSString *leftReuse = @"leftReuse";
        LCZLeftColumnCell *leftCell = [tableView dequeueReusableCellWithIdentifier:leftReuse];
        if (!leftCell) {
            leftCell = [[LCZLeftColumnCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:leftReuse];
        }
        
        leftCell.titleLabel.text = self.bigArr[indexPath.row + 1][@"cName"];
        leftCell.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        leftCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return leftCell;
        
    } else {
        
        LCZrightColumnCell *rightCell = [tableView dequeueReusableCellWithIdentifier:@"rightReuse" forIndexPath:indexPath];
        
        rightCell.titleLabel.text = self.mainArr[indexPath.row + 1][@"tname"];
        rightCell.dingLabel.text = [NSString stringWithFormat:@"%@订阅", self.mainArr[indexPath.row + 1][@"subnum"]];
        return rightCell;        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftTableView) {
        
        return self.bigArr.count - 1;
    } else {
        
        return self.mainArr.count - 1;
    }
}

#pragma mark tableView的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.leftTableView) {
        
        self.mainArr = self.bigArr[indexPath.row + 1][@"tList"];
        [self.rightTableView reloadData];
        self.rightTableView.contentOffset = CGPointMake(0, 0);
        
    } else {
        
        // 代理3
        [self.delegate push:self.mainArr[indexPath.row + 1][@"tid"]];
        
    }
}

@end
